package com.swlz.service.impl;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.swlz.config.AlipayConfig;
import com.swlz.config.GoFastDfsConfig;
import com.swlz.config.SystemConfig;
import com.swlz.dao.ConfigDAO;
import com.swlz.entity.Config;
import com.swlz.exception.CodeException;
import com.swlz.service.ConfigService;
import com.swlz.vo.PageVO;

/**
 * <pre>
 * 业务逻辑接口具体实现类
 * 系统配置
 * ClassName : com.swlz.service.impl.ConfigServiceImpl
 * Author : swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020-06-15 04:36:08
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 **/
public class ConfigServiceImpl implements ConfigService {
	private static Logger log = LoggerFactory.getLogger(ConfigServiceImpl.class);
	
	private ConfigDAO configDAO = new ConfigDAO();

	@Override
	public int add(Config config) throws CodeException {
		try {
			return configDAO.insertSelective(config);
		} catch (SQLException e) {
			log.warn("添加系统配置错误",e);
			throw new CodeException(500, "添加系统配置错误",e);
		}
	}

	@Override
	public int update(Config config) throws CodeException {
		try {
			return configDAO.updateSelective(config);
		} catch (SQLException e) {
			log.warn("修改系统配置错误",e);
			throw new CodeException(501, "修改系统配置错误",e);
		}
	}

	@Override
	public int delete(String configKey) throws CodeException {
		try {
			return configDAO.deleteById(configKey);
		} catch (SQLException e) {
			log.warn("删除系统配置错误",e);
			throw new CodeException(502, "删除系统配置错误",e);
		}
	}

	@Override
	public Config get(String configKey) throws CodeException {
		try {
			return configDAO.selectById(configKey);
		} catch (SQLException e) {
			log.warn("获取系统配置失败",e);
			throw new CodeException(503, "获取系统配置失败",e);
		}
	}

	@Override
	public PageVO<Config> page(Config config, Integer current, Integer rowMax) throws CodeException {
		try {
			long total = configDAO.countByModel(config);
			PageVO<Config> page = new PageVO<>(total, current, rowMax);
			return page.setList(configDAO.selectByModel(config, page.getOffset(), page.getRowMax()));
		} catch (SQLException e) {
			log.warn("获取系统配置分页数据失败",e);
			throw new CodeException(504, "获取系统配置分页数据失败",e);
		}
	}
	
	@Override
	public List<Config> list(Config config, int len) {
		try {
			return configDAO.selectByModel(config, 0, len);
		} catch (SQLException e) {
			log.warn("获取系统配置列表数据失败",e);
			throw new CodeException(506, "获取系统配置列表数据失败",e);
		}	
	}

	@Override
	public void save(Object config) throws CodeException {
		String key = config.getClass().getName();
		Config c = new Config();
		c.setConfigKey(key);
		c.setConfigValue(JSON.toJSONString(config));
		try{
			configDAO.insertSelective(c);
		}catch(Exception e){
			log.debug("保存配置信息失败",e);
			try {
				configDAO.updateSelective(c);
			} catch (SQLException e1) {
				log.warn("保存配置信息失败",e1);
				throw new CodeException(507, "保存配置信息失败",e1);
			}
		}
	}

	@Override
	public <T> T get(Class<T> cls)throws CodeException {
		try{
			String value = configDAO.selectById(cls.getName()).getConfigValue();
			return JSON.parseObject(value, cls);
		}catch(Exception ex){
			try {
				return (T)cls.newInstance();
			} catch (Exception e) {
				log.warn("获取配置信息失败",e);
				throw new CodeException(506, "获取配置信息成功");
			} 
		}
	}

	private static AlipayConfig alipayConfig = null;
	@Override
	public AlipayConfig getAlipayConfig() {
		if(alipayConfig==null){
			alipayConfig = get(AlipayConfig.class);
		}
		return alipayConfig;
	}

	private static GoFastDfsConfig goFastDfsConfig = null;
	@Override
	public GoFastDfsConfig getGoFastDfsConfig() {
		if(goFastDfsConfig == null){
			goFastDfsConfig = get(GoFastDfsConfig.class);
		}
		return goFastDfsConfig;
	}

	private static SystemConfig systemConfig = null;
	@Override
	public SystemConfig getSystemConfig() {
		if(systemConfig==null){
			systemConfig = get(SystemConfig.class);
		}
		return systemConfig;
	}


	@Override
	public int getCount() {
		try {
			Config config = configDAO.selectById("count");
			return Integer.valueOf(config.getConfigValue());
		}catch (Exception e) {
			return 0;
		}
		
	}

	@Override
	public void saveCount(int count) {
		Config config = null;
		try {
			config = configDAO.selectById("count");
			config.setConfigValue(count+"");
			configDAO.update(config);
		}catch (Exception e) {
			config = new Config();
			config.setConfigKey("count");
			config.setConfigValue(count+"");
			config.setCreateTime(new Date());
			config.setSortNumber(0L);
			config.setVersionNum(0);
			try {
				configDAO.insert(config);
			} catch (SQLException e1) {
				
			}
		}
		
	}


}
